﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

using System.Data.SqlClient;
using System.Data;
using Microsoft.ApplicationBlocks.Data;



namespace _619BJP.Database
{
    

  public  class adminloginDB
    {
      private string connString = System.Configuration.ConfigurationManager.ConnectionStrings["619BJP.Data.ConnectionString"].ConnectionString;
       


      /// <summary>
      /// 登陆验证
      /// </summary>
      /// <param name="username"></param>
      /// <param name="password"></param>
      /// <returns></returns>
       public int CheckAdmin(string username,string password)
       {

           using (SqlConnection conn = new SqlConnection(connString))
           {
               //DataRowCollection drUsers;
               conn.Open();
               string sql = "select * from SY_Admin where name= @Username And password = @Password";
               SqlParameter[] paramsToSql = new SqlParameter[2];
               paramsToSql[0] = new SqlParameter("@Username", SqlDbType.NVarChar);
               paramsToSql[0].Value = username;
               paramsToSql[1] = new SqlParameter("@Password", SqlDbType.NVarChar);
               paramsToSql[1].Value = password;

               SqlDataReader drUsers = SqlHelper.ExecuteReader(conn, CommandType.Text, sql, paramsToSql);

               if (drUsers.Read())
               {
                   return int.Parse(drUsers["ID"].ToString());
               }
               else
               {
                   return -1;
               }
           }
       }

       public void AdminLoginUpdate(string username)
       {
           using (SqlConnection conn = new SqlConnection(connString))
           {
               DataRowCollection drUsers;
               conn.Open();
               string sql = "UPDATE [SY_Admin] SET [last_logon_time] =@lastlogontime,[last_logon_ip] =@lastlogonip ,[logon_times] =logon_times+1 WHERE name=@name";
               SqlParameter[] paramsToSql = new SqlParameter[3];
               paramsToSql[0] = new SqlParameter("@lastlogontime", SqlDbType.DateTime);
               paramsToSql[0].Value = System.DateTime.Now;
               paramsToSql[1] = new SqlParameter("@lastlogonip", SqlDbType.NVarChar);
               paramsToSql[1].Value = "127.0.0.1";
               paramsToSql[2] = new SqlParameter("@name", SqlDbType.NVarChar);
               paramsToSql[2].Value = username;

               SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql, paramsToSql);



           }
       
       }

    }
}
